Count different palindromic subsequences¶
Time: O(N^2); Space: O(N); hard
Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.
A subsequence of a string S is obtained by deleting 0 or more characters from S.
A sequence is palindromic if it is equal to the sequence reversed.
Two sequences A_1, A_2, … and B_1, B_2, … are different if there is some i for which A_i != B_i.
Example 1:
Input: S = ‘bccb’
Output: 6
Explanation:
The 6 different non-empty palindromic subsequences are ‘b’, ‘c’, ‘bb’, ‘cc’, ‘bcb’, ‘bccb’.
Note that ‘bcb’ is counted only once, even though it occurs twice.
Example 2:
Input: S = ‘abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba’
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.
Constraints:
The length of S will be in the range [1, 1000].
Each character S[i] will be in the set {‘a’, ‘b’, ‘c’, ‘d’}.
Hints:
Let dp(i, j) be the answer for the string T = S[i:j+1] including the empty sequence. The answer is the number of unique characters in T, plus palindromes of the form “a_a”, “b_b”, “c_c”, and “d_d”, where “_” represents zero or more characters.
1. Dynamic Programming¶
[40]:
class Solution1(object):
def countPalindromicSubsequences(self, S):
"""
:type S: str
:rtype: int
"""
def dp(i, j, prv, nxt, lookup):
if lookup[i][j] != -1:
return lookup[i][j]
result = 1
if i <= j:
for x in range(4):
i0 = nxt[i][x]
j0 = prv[j][x]
if i <= i0 <= j:
result = (result + 1) % P
if -1 < i0 < j0:
result = (result + dp(i0+1, j0-1, prv, nxt, lookup)) % P
result %= P
lookup[i][j] = result
return result
prv = [-1] * len(S)
nxt = [-1] * len(S)
last = [-1] * 4
for i in range(len(S)):
last[ord(S[i]) - ord('a')] = i
prv[i] = tuple(last)
last = [-1] * 4
for i in reversed(range(len(S))):
last[ord(S[i]) - ord('a')] = i
nxt[i] = tuple(last)
P = 10**9 + 7
lookup = [[-1] * len(S) for _ in range(len(S))]
return dp(0, len(S)-1, prv, nxt, lookup) - 1
[41]:
s = Solution1()
S = 'bccb'
assert s.countPalindromicSubsequences(S) == 6
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
assert s.countPalindromicSubsequences(S) == 104860361
See also:¶
https://leetcode.com/problems/count-different-palindromic-subsequences
https://www.lintcode.com/problem/count-different-palindromic-subsequences/description
Related problems:¶
https://leetcode.com/problems/longest-palindromic-subsequence/